package com.tz.cartelo.fragment;

import android.media.Image;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tz.cartelo.R;
import com.tz.cartelo.activity.GoodsClassifyActivity;
import com.tz.cartelo.activity.GoodsDetailsActivity;
import com.tz.cartelo.adapter.GoodsCenterFragmentAdapter;
import com.tz.cartelo.constant.Api;
import com.tz.cartelo.constant.Key;
import com.tz.cartelo.model.DataParams;
import com.tz.cartelo.model.GoodsBannerList;
import com.tz.cartelo.model.GoodsList;
import com.tz.cartelo.result.ResultGoodsCenter;
import com.tz.cartelo.util.CommonUtils;
import com.tz.cartelo.view.GlideImageLoader;
import com.tz.common.util.LogUtil;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.List;

import cn.bingoogolapple.refreshlayout.BGARefreshLayout;
import in.srain.cube.views.GridViewWithHeaderAndFooter;

/**
 * Created by ok on 2016/9/12.
 */
public class GoodsCenterFragment extends DataLoadFragment implements BGARefreshLayout.BGARefreshLayoutDelegate, View.OnClickListener {
    private static final String TAG = "GoodsCenterFragment";
    private static final String IS_ALL = "0";
    private static final String IS_NEW = "1";
    private boolean mSaleStatus;
    private static final String SALE_BIG = "1";//1销量 大-小
    private static final String SALE_SMALL = "-1";//-1销量 小-大
    private boolean mStockStatus;
    private static final String STOCK_BIG = "2";//2库存 大-小
    private static final String STOCK_SMALL = "-2";//-2库存 小-大
    private boolean mPriceStatus;
    private static final String PRICE_BIG = "3";//3价格 大-小
    private static final String PRICE_SMALL = "-3";//-3价格 小-大
    private int mSelectStatus;
    private static final int IS_ALL_STATUS = 0;
    private static final int IS_NEW_STATUS = 1;
    private static final int SALE_BIG_STATUS = 2;
    private static final int SALE_SMALL_STATUS = 3;
    private static final int STOCK_BIG_STATUS = 4;
    private static final int STOCK_SMALL_STATUS = 5;
    private static final int PRICE_BIG_STATUS = 6;
    private static final int PRICE_SMALL_STATUS = 7;
    private BGARefreshLayout mRefreshLayout;
    private GoodsCenterFragmentAdapter mGoodsCenterFragmentAdapter;
    private List<String> mImgUrlList;
    private List<GoodsList> mGoodsListList;
    private List<GoodsBannerList> mGoodsBannerListList;
    private int mPageNo;
    private String mSort;
    private String mIsNew;
    private Banner mBanner;
    private TextView mGoodsCenterAll, mGoodsCenterNew, mGoodsCenterSale, mGoodsCenterStock, mGoodsCenterPrice;
    private LinearLayout mGoodsCenterSaleLayout, mGoodsCenterStockLayout, mGoodsCenterPriceLayout;
    private ImageView mGoodsCenterSaleArrowTop, mGoodsCenterSaleArrowDown, mGoodsCenterStockArrowTop, mGoodsCenterStockArrowDown, mGoodsCenterPriceArrowTop, mGoodsCenterPriceArrowDown;
    private boolean mCanLoadMore = true;
    private GridViewWithHeaderAndFooter goodsCenterList;
    private boolean isFirstLoad = true;

    @Override
    protected int getLayoutResId() {
        return R.layout.main_goods_center_fragment;
    }

    @Override
    protected void init(View view) {
//        EventBus.getDefault().register(this);

        view.findViewById(R.id.goods_center_classify_img).setOnClickListener(this);
        mRefreshLayout = (BGARefreshLayout) view.findViewById(R.id.goods_center_fresh_layout);
        mRefreshLayout.setDelegate(this);
        CommonUtils.setBGARefreshLayout(mRefreshLayout, getActivity());

        final ImageView toTopImg = (ImageView) view.findViewById(R.id.to_top_img);
        toTopImg.setOnClickListener(this);
        goodsCenterList = (GridViewWithHeaderAndFooter) view.findViewById(R.id.goods_center_list);
        View headerView = LayoutInflater.from(getActivity()).inflate(R.layout.item_goods_center_list_head, null);
        goodsCenterList.addHeaderView(headerView);
        mImgUrlList = new ArrayList<>();
        mGoodsListList = new ArrayList<>();
        mGoodsBannerListList = new ArrayList<>();
        mGoodsCenterFragmentAdapter = new GoodsCenterFragmentAdapter(getActivity(), mGoodsListList);
        goodsCenterList.setAdapter(mGoodsCenterFragmentAdapter);
        goodsCenterList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Bundle bundle = new Bundle();
                bundle.putString(Key.GOODS_ID, mGoodsListList.get(i).getGoodsId());
                switchActivity(GoodsDetailsActivity.class, bundle, Key.CLASSIFY_GOODS);
            }
        });

        goodsCenterList.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem != 0) {
                    toTopImg.setVisibility(View.VISIBLE);
                } else {
                    toTopImg.setVisibility(View.GONE);
                }
            }
        });

        mBanner = (Banner) headerView.findViewById(R.id.goods_center_banner);
        mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        mBanner.isAutoPlay(true);
        mBanner.setDelayTime(Key.BANNER_TIME);
        mBanner.setImageLoader(new GlideImageLoader());

        mGoodsCenterAll = (TextView) headerView.findViewById(R.id.goods_center_all);
        mGoodsCenterNew = (TextView) headerView.findViewById(R.id.goods_center_new);
        mGoodsCenterSale = (TextView) headerView.findViewById(R.id.goods_center_sale);
        mGoodsCenterStock = (TextView) headerView.findViewById(R.id.goods_center_stock);
        mGoodsCenterPrice = (TextView) headerView.findViewById(R.id.goods_center_price);
        mGoodsCenterSaleLayout = (LinearLayout) headerView.findViewById(R.id.goods_center_sale_layout);
        mGoodsCenterSaleArrowTop = (ImageView) headerView.findViewById(R.id.goods_center_sale_arrow_top);
        mGoodsCenterSaleArrowDown = (ImageView) headerView.findViewById(R.id.goods_center_sale_arrow_down);
        mGoodsCenterStockLayout = (LinearLayout) headerView.findViewById(R.id.goods_center_stock_layout);
        mGoodsCenterStockArrowTop = (ImageView) headerView.findViewById(R.id.goods_center_stock_arrow_top);
        mGoodsCenterStockArrowDown = (ImageView) headerView.findViewById(R.id.goods_center_stock_arrow_down);
        mGoodsCenterPriceLayout = (LinearLayout) headerView.findViewById(R.id.goods_center_price_layout);
        mGoodsCenterPriceArrowTop = (ImageView) headerView.findViewById(R.id.goods_center_price_arrow_top);
        mGoodsCenterPriceArrowDown = (ImageView) headerView.findViewById(R.id.goods_center_price_arrow_down);

        mGoodsCenterAll.setOnClickListener(this);
        mGoodsCenterNew.setOnClickListener(this);
        mGoodsCenterSaleLayout.setOnClickListener(this);
        mGoodsCenterStockLayout.setOnClickListener(this);
        mGoodsCenterPriceLayout.setOnClickListener(this);

    }

    @Override
    protected void initTitle(View view) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.goods_center_classify_img:
                switchActivity(GoodsClassifyActivity.class, null, Key.CLASSIFY_GOODS);
                break;
            case R.id.to_top_img:
                goodsCenterList.smoothScrollToPositionFromTop(0, 0);
                break;
            case R.id.goods_center_all:
                if (mSelectStatus != IS_ALL_STATUS) {
                    mSelectStatus = IS_ALL_STATUS;
                    mIsNew = IS_ALL;
                    mPageNo = 1;
                    mGoodsListList.clear();
                    mGoodsBannerListList.clear();
                    mImgUrlList.clear();
                    loadData(Api.GOODS_CENTER, true);

                    mGoodsCenterAll.setBackgroundResource(R.drawable.shape_stroke_main_left_frame_gray);
                    mGoodsCenterAll.setTextColor(getResources().getColor(R.color.black));
                    mGoodsCenterNew.setBackgroundResource(R.drawable.shape_stroke_main_right_fill_gray);
                    mGoodsCenterNew.setTextColor(getResources().getColor(R.color.white));
                }
                break;
            case R.id.goods_center_new:
                if (mSelectStatus != IS_NEW_STATUS) {
                    mSelectStatus = IS_NEW_STATUS;
                    mIsNew = IS_NEW;
                    mPageNo = 1;
                    mGoodsListList.clear();
                    mGoodsBannerListList.clear();
                    mImgUrlList.clear();
                    loadData(Api.GOODS_CENTER, true);

                    mGoodsCenterAll.setBackgroundResource(R.drawable.shape_stroke_main_left_fill_gray);
                    mGoodsCenterAll.setTextColor(getResources().getColor(R.color.white));
                    mGoodsCenterNew.setBackgroundResource(R.drawable.shape_stroke_main_right_frame_gray);
                    mGoodsCenterNew.setTextColor(getResources().getColor(R.color.black));
                }
                break;
            case R.id.goods_center_sale_layout:
                if (mSelectStatus != SALE_BIG_STATUS) {
                    mSelectStatus = SALE_BIG_STATUS;
                    mSort = SALE_BIG;
                    mGoodsListList.clear();
                    mGoodsBannerListList.clear();
                    mImgUrlList.clear();
                    mPageNo = 1;
                    loadData(Api.GOODS_CENTER, true);
                    mGoodsCenterSaleArrowTop.setImageResource(R.mipmap.ic_arrow_top_black);
                    mGoodsCenterSaleArrowDown.setImageResource(R.mipmap.ic_arrow_down_gray);
                } else {
                    mSelectStatus = SALE_SMALL_STATUS;
                    mSort = SALE_SMALL;
                    mPageNo = 1;
                    mGoodsListList.clear();
                    mGoodsBannerListList.clear();
                    mImgUrlList.clear();
                    loadData(Api.GOODS_CENTER, true);
                    mGoodsCenterSaleArrowTop.setImageResource(R.mipmap.ic_arrow_top_gray);
                    mGoodsCenterSaleArrowDown.setImageResource(R.mipmap.ic_arrow_down_black);
                }
                mGoodsCenterSaleLayout.setBackgroundResource(R.drawable.shape_stroke_main_left_frame_gray);
                mGoodsCenterSale.setTextColor(getResources().getColor(R.color.black));
                mGoodsCenterStockLayout.setBackgroundResource(R.drawable.shape_stroke_main_center_fill_gray);
                mGoodsCenterStock.setTextColor(getResources().getColor(R.color.white));
                mGoodsCenterPriceLayout.setBackgroundResource(R.drawable.shape_stroke_main_right_fill_gray);
                mGoodsCenterPrice.setTextColor(getResources().getColor(R.color.white));

                mGoodsCenterStockArrowTop.setImageResource(R.mipmap.ic_arrow_top_white);
                mGoodsCenterStockArrowDown.setImageResource(R.mipmap.ic_arrow_down_white);
                mGoodsCenterPriceArrowTop.setImageResource(R.mipmap.ic_arrow_top_white);
                mGoodsCenterPriceArrowDown.setImageResource(R.mipmap.ic_arrow_down_white);
                break;
            case R.id.goods_center_stock_layout:
                if (mSelectStatus != STOCK_BIG_STATUS) {
                    mSelectStatus = STOCK_BIG_STATUS;
                    mSort = STOCK_BIG;
                    mGoodsListList.clear();
                    mGoodsBannerListList.clear();
                    mImgUrlList.clear();
                    mPageNo = 1;
                    loadData(Api.GOODS_CENTER, true);
                    mGoodsCenterStockArrowTop.setImageResource(R.mipmap.ic_arrow_top_black);
                    mGoodsCenterStockArrowDown.setImageResource(R.mipmap.ic_arrow_down_gray);
                } else {
                    mSelectStatus = STOCK_SMALL_STATUS;
                    mSort = STOCK_SMALL;
                    mPageNo = 1;
                    mGoodsListList.clear();
                    mGoodsBannerListList.clear();
                    mImgUrlList.clear();
                    loadData(Api.GOODS_CENTER, true);
                    mGoodsCenterStockArrowTop.setImageResource(R.mipmap.ic_arrow_top_gray);
                    mGoodsCenterStockArrowDown.setImageResource(R.mipmap.ic_arrow_down_black);
                }
                mGoodsCenterSaleLayout.setBackgroundResource(R.drawable.shape_stroke_main_left_fill_gray);
                mGoodsCenterSale.setTextColor(getResources().getColor(R.color.white));
                mGoodsCenterStockLayout.setBackgroundResource(R.drawable.shape_stroke_main_center_frame_gray);
                mGoodsCenterStock.setTextColor(getResources().getColor(R.color.black));
                mGoodsCenterPriceLayout.setBackgroundResource(R.drawable.shape_stroke_main_right_fill_gray);
                mGoodsCenterPrice.setTextColor(getResources().getColor(R.color.white));

                mGoodsCenterSaleArrowTop.setImageResource(R.mipmap.ic_arrow_top_white);
                mGoodsCenterSaleArrowDown.setImageResource(R.mipmap.ic_arrow_down_white);
                mGoodsCenterPriceArrowTop.setImageResource(R.mipmap.ic_arrow_top_white);
                mGoodsCenterPriceArrowDown.setImageResource(R.mipmap.ic_arrow_down_white);
                break;
            case R.id.goods_center_price_layout:
                if (mSelectStatus != PRICE_BIG_STATUS) {
                    mSelectStatus = PRICE_BIG_STATUS;
                    mSort = PRICE_BIG;
                    mGoodsListList.clear();
                    mGoodsBannerListList.clear();
                    mImgUrlList.clear();
                    mPageNo = 1;
                    loadData(Api.GOODS_CENTER, true);
                    mGoodsCenterPriceArrowTop.setImageResource(R.mipmap.ic_arrow_top_black);
                    mGoodsCenterPriceArrowDown.setImageResource(R.mipmap.ic_arrow_down_gray);
                } else {
                    mSelectStatus = PRICE_SMALL_STATUS;
                    mSort = PRICE_SMALL;
                    mPageNo = 1;
                    mGoodsListList.clear();
                    mGoodsBannerListList.clear();
                    mImgUrlList.clear();
                    loadData(Api.GOODS_CENTER, true);
                    mGoodsCenterPriceArrowTop.setImageResource(R.mipmap.ic_arrow_top_gray);
                    mGoodsCenterPriceArrowDown.setImageResource(R.mipmap.ic_arrow_down_black);
                }
                mGoodsCenterSaleLayout.setBackgroundResource(R.drawable.shape_stroke_main_left_fill_gray);
                mGoodsCenterSale.setTextColor(getResources().getColor(R.color.white));
                mGoodsCenterStockLayout.setBackgroundResource(R.drawable.shape_stroke_main_center_fill_gray);
                mGoodsCenterStock.setTextColor(getResources().getColor(R.color.white));
                mGoodsCenterPriceLayout.setBackgroundResource(R.drawable.shape_stroke_main_right_frame_gray);
                mGoodsCenterPrice.setTextColor(getResources().getColor(R.color.black));

                mGoodsCenterSaleArrowTop.setImageResource(R.mipmap.ic_arrow_top_white);
                mGoodsCenterSaleArrowDown.setImageResource(R.mipmap.ic_arrow_down_white);
                mGoodsCenterStockArrowTop.setImageResource(R.mipmap.ic_arrow_top_white);
                mGoodsCenterStockArrowDown.setImageResource(R.mipmap.ic_arrow_down_white);
                break;
            default:
                break;
        }

    }

    @Override
    protected void initData() {
        mSelectStatus = IS_ALL_STATUS;
        mSort = SALE_BIG;
        mIsNew = IS_ALL;
        mPageNo = 1;
        mGoodsListList.clear();
        mGoodsBannerListList.clear();
        mImgUrlList.clear();
        loadData(Api.GOODS_CENTER, true);
    }

    @Override
    protected void initParams(DataParams params) {
        params.addParam("pageNo", mPageNo + "");
        params.addParam("sort", mSort);
        params.addParam("isNew", mIsNew);//1：是新品，0全部
    }

    @Override
    protected void disposeResult(Api api, String response) {
        LogUtil.d(TAG, response);
        switch (api) {
            case GOODS_CENTER:
                //结束刷新和加载
                mRefreshLayout.endRefreshing();
                mRefreshLayout.endLoadingMore();

                ResultGoodsCenter resultClassClassify = fromJson(response, ResultGoodsCenter.class);
                if (!resultClassClassify.isSuccess()) {
                    showToast(resultClassClassify.getMessage());
                    return;
                }
                showData(resultClassClassify);
                break;
            default:
                break;
        }
    }

    @Override
    protected void errorResponse() {
        mRefreshLayout.endRefreshing();
        mRefreshLayout.endLoadingMore();
    }

    private void showData(ResultGoodsCenter resultClassClassify) {
        mGoodsListList.addAll(resultClassClassify.getGoodsCenter().getGoodsListList());
        mGoodsCenterFragmentAdapter.notifyDataSetChanged();
        if (mPageNo > 1) {
            if (resultClassClassify.getGoodsCenter().getGoodsListList().isEmpty()) {
                mCanLoadMore = false;
                mPageNo--;
            }
        }

        if (isFirstLoad) {
            mGoodsBannerListList.addAll(resultClassClassify.getGoodsCenter().getGoodsBannerListList());
            for (GoodsBannerList goodsBannerList : mGoodsBannerListList) {
                mImgUrlList.add(goodsBannerList.getPictureUrl());
            }
            mBanner.setImages(mImgUrlList);
            mBanner.start();
            isFirstLoad = false;
        }
    }


    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        mPageNo = 1;
        mGoodsListList.clear();
        mGoodsBannerListList.clear();
        mImgUrlList.clear();
        mCanLoadMore = true;
        loadData(Api.GOODS_CENTER, true);
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        if (!mCanLoadMore) {
            mRefreshLayout.endLoadingMore();
            return false;
        } else {
            mPageNo++;
            loadData(Api.GOODS_CENTER, true);
            return true;
        }
    }

    // 接收函数
//    public void onEvent(EventUtil event) {
//        switch (event.getMsg()) {
//            default:
//                break;
//        }
//    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        EventBus.getDefault().unregister(this);//取消注册
    }

}
